// pages/addcustomer/addcustomer.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:0,
      bank:"",
      bank_address:"",
      account:"",
      swift:"",
      religion:"",
      email:"",
      phone:"",
      address:"",
      business_scope:"",
      license:"",
      autosize:{maxHeight:100,minHeight:50},
      scope_data:['A','B'],
      showPop:false,
      fileList:[],
      business_index:0
  },
  selectScope(events){
      this.setData({
        business_index:events.detail.value,
        business_scope:this.data.scope_data[events.detail.value]
      })
      this.onClose()
  },
  showPopup() {
    this.setData({ showPop: true });
  },

  onClose() {
    this.setData({ showPop: false });
  },
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    this.setData({
      [valueName]:event.detail
    });
  },
  goPage(event){
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }
  },
  submit(){
    var that = this;
    var that = this;
    var data = that.data;
    request.requestAjax('account/add',data,"加载中",function(res){
      if(res.status==200){
        wx.showToast({
          title: '保存成功',
          icon:"success"
        })
        setTimeout(function(){
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            var account = res.data;
            prevPage.setData({
              account:account
            })
            if(Reflect.has(prevPage,"getList")){
              prevPage.getList();
            }
       
            wx.navigateBack();
        },1000);
          return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }

  },function(res){
    wx.showToast({
      title: '提交失败',
      icon:"none"
    })
  });
  },
  getBusinessScope(){
    var that = this;
    request.requestAjax('customer/getBusinessScope',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              scope_data:res.data
            })
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  delPic(e){
    var that = this;
    let fileList = that.data.fileList;
    fileList.splice(e.detail.index,1);
    that.setData({
      fileList
    })
  },
  showPic(e){
    return true;
  },
  afterRead(event) {
    var that = this;
    const { file } = event.detail;
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: "图片上传中",
    })
      

    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://www.jufupen.online/api/upload/image', // 仅为示例，非真实的接口地址
      filePath: file.url,
      header: {
        'Authori-zation':"Bearer "+wx.getStorageSync('token'),
      },
      name: 'file',
      formData: { },
      success(res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        // 上传完成需要更新 fileList
        const { fileList = [] } = that.data;
        fileList.push({ ...file, url: res.data.url });
        that.setData({ fileList });
      },
      fail(){
        wx.showToast({
          title: '上传失败',
          icon:"error"
        })
        wx.hideLoading()
        wx.hideNavigationBarLoading()
      }
    });
  },
  getInfo(){
    var that = this;
    request.requestAjax('account/info',{id:this.data.id},"加载中",function(res){
        if(res.status==200){
            that.setData({
              bank:res.data.bank,
              bank_address:res.data.bank_address,
              account:res.data.account,
              swift:res.data.swift,
          });
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBusinessScope();
    if(options.id!=undefined){
      this.setData({id:options.id})
      this.getInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})